package com.chixing.day12_date_factory;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;

import static java.time.ZonedDateTime.now;

public class DateDemo {
    public static void main(String[] args) {
        //创建指定日期
        LocalDate date = LocalDate.of(1985, 10, 12);
        System.out.println("指定的时间是：" + date);
        //得到当前时间
        LocalDate now = LocalDate.now();
        System.out.println("当前日期：" + now);
        //获取日期信息
        System.out.println("年:" + now.getYear());
        System.out.println("月:" + now.getMonthValue());
        System.out.println("日:" + now.getDayOfMonth());
        System.out.println("星期:" + now.getDayOfWeek());
        System.out.println("一年中的第:" + now.getDayOfYear() + "天");
    }

    public void testLocalTime() {
        //得到指定时间
        LocalTime time = LocalTime.of(9, 54, 32, 129_900_000);
        System.out.println("指定日期是：" + time);
        //得到当前时间
        LocalTime now = LocalTime.now();
        System.out.println("当前时间：" + now);
        //获取时间信息
        System.out.println("小时:" + now.getHour());
        System.out.println("分钟:" + now.getMinute());
        System.out.println("秒:" + now.getSecond());
        System.out.println("纳秒:" + now.getNano());
    }

    //LocalDatetime类：获取日期时间信息
    public void testLocalDateTime() {
        //获取指定日期时间
        LocalDateTime dateTime = LocalDateTime.of(2018, 9, 23, 10, 10, 45, 730);
        System.out.println("指定时间日期是：" + dateTime);
        //得到当前日期时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前日期时间是：" + now);
        //获取日期时间
        System.out.println("年:" + now.getYear());
        System.out.println("月:" + now.getMonthValue());
        System.out.println("日:" + now.getDayOfMonth());
        System.out.println("小时:" + now.getHour());
        System.out.println("分钟:" + now.getMinute());
        System.out.println("秒:" + now.getSecond());
        System.out.println("纳秒:" + now.getNano());
    }

    //LocalDateTime类 ： 对日期时间的修改
    public void testUpdate() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前系统日期时间：" + now);
        //修改日期时间
        LocalDateTime year = now.withYear(2071);
        System.out.println("修改的年份是：" + year);
        System.out.println("now == year" + (now == year));
        System.out.println("修改的月份:" + now.withMonth(9));
        System.out.println("修改的日:" + now.withDayOfMonth(28));
        System.out.println("修改的小时:" + now.withHour(12));
        System.out.println("修改的分钟:" + now.withMonth(20));
        System.out.println("修改的秒:" + now.withSecond(45));
        System.out.println("修改的纳秒:" + now.withNano(750));

        //在当前对象上加上或减去指定的时间
        LocalDateTime localDateTime = now.plusDays(5);
        System.out.println("五天后:" + localDateTime);
        System.out.println("十年后：" + now.plusYears(10));
        System.out.println("20个月后：" + now.plusMonths(20));

        System.out.println("20年前：" + now.minusYears(20));
        System.out.println("5个月前：" + now.minusMonths(5));
        System.out.println("100天前：" + now.minusDays(100));
    }

    //时间比较 在JDK8中，LocalDate类中使用isBefore(),isAfter(),equals()方法来比较
//两个日期，可直接进行比较
    public void testCompare() {
        LocalDate now = LocalDate.now();
        LocalDate of = LocalDate.of(2019, 8, 23);
        System.out.println(now.equals(of)); //false
        System.out.println(now.isAfter(of)); //true
        System.out.println(now.isBefore(of)); //false
    }

    //DateTimeFormatter类进行时间日期格式化
    public void testFormat() {
        //获得当前时间
        LocalDateTime now = LocalDateTime.now();
        //将当前时间格式化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        //将日期的时间格式化为字符串
        String dateTime = now.format(formatter);
        System.out.println("格式化后的时间日期" + dateTime);
        //将字符串解析为日期时间
        LocalDateTime parse = LocalDateTime.parse("2024-09-04 08:29:13", formatter);
        System.out.println("解析后的日期时间：" + parse);
    }

    //Instant类 时间戳/时间线
    public void testInstant() {
        Instant now = Instant.now();
        System.out.println("当前时间戳：" + now);
        //获取从1970年1月1日00:00:00的秒
        System.out.println(now.getNano());
        System.out.println(now.getEpochSecond());
        System.out.println(now.toEpochMilli());
        System.out.println(System.currentTimeMillis());
        Instant instant = Instant.ofEpochSecond(5);
        Instant instant1 = Instant.ofEpochMilli(5);
        System.out.println(instant1);
        System.out.println(instant1);
    }

    //JDK8的计算日期时间差类
//1.Duration:用于计算2个时间(LocalTime，时分秒)的距离
//2.Period:用于计算2个日期(LocalDate,年月日)的距离
    public void testCha() {
        //Duration计算时间的距离
        LocalTime now = LocalTime.now();
        LocalTime time = LocalTime.of(14, 15, 20);
        Duration d = Duration.between(now, time);
        System.out.println("相差的天数:" + d.toDays());
        System.out.println("相差的小时数:" + d.toHours());
        System.out.println("相差的分钟:" + d.toMinutes());
        System.out.println("相差的秒数:" + d.toMillis());
        //Period计算日期的距离+
        LocalDate date = LocalDate.now();
        LocalDate date2 = LocalDate.of(2022, 12, 20);
        Period period = Period.between(date, date2);
        System.out.println("相差的年 ： " + period.getYears());
        System.out.println("相差的月 ： " + period.getMonths());
        System.out.println("相差的日  ： " + period.getDays());
    }
    //时间校正器 TemporalAdjuster

    public void testTemporalAdjuster() {
        LocalDateTime now = LocalDateTime.now();
        //得到下个月的第一天
        TemporalAdjuster firstWeekDayofNextMonth = temporal -> {
            LocalDateTime dateTime = (LocalDateTime) temporal;
            LocalDateTime nextMonth = dateTime.plusMonths(1).withDayOfMonth(1);
            System.out.println("下个月的第一天 ： " + nextMonth);
            return nextMonth;
        };
        LocalDateTime nextMonth = now.with(firstWeekDayofNextMonth);
        System.out.println("nextMonth :" + nextMonth);
    }

    //设置日期时间的时区

    public void testZone() {
        //1.获取所有的时区ID
        //ZoneId.getAvailableZoneIds().forEach(System.out::println);
        //不带时间，获取计算机当前时间 中国使用的东八区的时区，比标准时间早8个小时
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now : " + now);
        //操作带时区的类
        ZonedDateTime zonedDateTime = now(Clock.systemUTC());
        System.out.println(zonedDateTime);
        ZonedDateTime now1 = now(Clock.systemUTC());
        System.out.println("now 1 : " + now1);
        //now():使用计算机的默认时区，创建日期时间
        ZonedDateTime now2 = now();
        System.out.println("now2 :" + now2);

//使用指定的时区创建日期时间
        ZonedDateTime now3 = now(ZoneId.of("America/Vancouver"));
        System.out.println("now3 : " + now3);
    }
}
